var tags=new Array();
var MaxTags=50;
var imgpath="";


function addTags(){
	var tag=$.trim($("#tagbox")[0].value);
	$("#tagbox")[0].value="";
	if (tag.length<1)
		return;
	else{
		
		tag=antiJs(tag);
		
		tagarray=tag.split(" ");
		for(i=0;i<tagarray.length;i++){
			var isNew=true;
			e=$.trim(tagarray[i]);
			if(""==e)
				continue;
			for(j=0;j<tags.length;j++){
				if (tags[j]==e){
					isNew=false;
					break;
				}
			}
			if(isNew)
				tags.push(e);
		}
	}
	showSelectTags(tags);
}

function showSelectTags(tags){
	$("#selectTags").html("");
	var taglen=1;
	var n=1;
	var content="<font style='font-weight: 600;'>已添加标签：</font><br/><br/>";
	for (i=0;i<tags.length;i++){
		if (""==tags[i])
			continue;
		content+="<span  class='text' id='tag"+i+"'><span >"+tags[i]+"</span><a class='btn_nouline' title='删除' color='red' href='javascript:delTag("+i+");'>X</a></span>";
		taglen+=tags[i].length+1;
		if((taglen-1)/24>n){
			content+="<br/></br>";
			n+=1;
		}
	}
	
	$("#selectTags").append(content);
}

function delTag(pos){
	tags[pos]="";
//	$("#tag"+pos).empty();
	showSelectTags(tags);
//	alert(tags);
}



function uploadImg(){ 
	var x = getCookie("user");
	if (x == "") {
		exitsys();
		showLoginWin();
		loginwarn("您尚未登录，请先登录，谢谢！");
		return;
	}
	$("#thephoto").empty();
	var content="<form id='upform' action='UploadImg' method='post' enctype='multipart/form-data' style='margin-left : 4px;'>" +
			
			"<table style='border-spacing:6px;'>"+
			"<tr><td id='upimgtip' style='color:red;' colspan='2'></td></tr>" +
			"<tr><td  class='upphoto'><input name='photo' type='file' />" +
			"<input type='button' value='\u4e0a\u4f20' onclick='uploadsubmit()' /><br/>" +
			"</td></tr></table></form>";
	showlayer(content);	
	$('#upform').ajaxForm(function(s) { 
		afterUpImg(s);
    }); 
}

function afterUpImg(rsp){
	if (rsp.indexOf("succ")>0) {
		closeTop();
	  var jqueryobj=$("#itemimg")[0]; 	  
	  imgpath=rsp.substring(9,rsp.length-2);
	  jqueryobj.src=imgpath+"?";
	} else {
		$("#upimgtip").html(rsp.substring(8,rsp.length-2));
	}
}

function uploadsubmit(){
	var x = getCookie("user");
	if (x == "") {
		exitsys();
		showLoginWin();
		loginwarn("您尚未登录，请先登录，谢谢！");
		return;
	}
	$("#upform").submit();
}
function showlayer(cont){
	$("#uppic").show();
	showTop(cont, 550, 200, 200);
	
}
function createSubmit(){
	x = getCookie("user");
	if (x == "") {
		exitsys();
		showLoginWin();
		loginwarn("您尚未登录，请先登录，谢谢！");
		return;
	}
	var name=$.trim($("#name")[0].value);
	if (name.length<1 || name.length>8)
	{
		$("#itemwarn").html("");
		$("#itemwarn").append("*\u8bf7\u586b\u5199\u59d3\u540d,\u59d3\u540d\u4e0d\u80fd\u8d85\u8fc74\u4e2a\u6c49\u5b57");
		return;
	}
	var tag="";
	for (i=0;i<tags.length;i++){
			if(tags[i]!=""){
				tag+=tags[i]+" ";
			}
	}
	
	var data = "name=" + name + "&tags="+tag+"&imgpath="+imgpath;

	$.ajax( {
		type : "post",
		url : "CreateItem",
		dataType : "json",
		data : data,
		success : function(rsp) {
			createItemCallback(rsp);
		}
	});
}

function createItemCallback(rsp){
	if(rsp["msg"]!=null){
		$("#itemwarn").html("");
		msg=rsp["msg"];
		if(rsp["msg"]=="-1"){
			msg="您尚未登录，请登录后再继续操作，谢谢！";
			exitsys();
			showLoginWin();
			loginwarn("您尚未登录，请先登录，谢谢！");
			return;
		}
		$("#itemwarn").append("*"+msg);
	}
	if(rsp["succ"]!=null){
		location.href="/178rp/jsp/item.jsp?id="+ rsp["succ"];
	}
}